package com.luguotao.lease.web.admin.service.impl;

import com.luguotao.lease.common.exception.LeaseException;
import com.luguotao.lease.model.entity.*;
import com.luguotao.lease.model.enums.ItemType;
import com.luguotao.lease.web.admin.mapper.*;
import com.luguotao.lease.web.admin.service.*;
import com.luguotao.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.luguotao.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.luguotao.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.luguotao.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.luguotao.lease.web.admin.vo.fee.FeeValueVo;
import com.luguotao.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
        @Autowired
        private ApartmentFacilityService apartmentFacilityService;
        @Autowired
        private ApartmentLabelService apartmentLabelService;
        @Autowired
        private ApartmentFeeValueService apartmentFeeValueService;
        @Autowired
        private GraphInfoService graphInfoService;
        @Autowired
        private ProvinceInfoMapper provinceInfoMapper;
        @Autowired
        private CityInfoMapper cityInfoMapper;
        @Autowired
        private DistrictInfoMapper districtInfoMapper;
        @Autowired
        private ApartmentInfoMapper apartmentInfoMapper;
        @Autowired
        private RoomInfoMapper roomInfoMapper;
        @Autowired
        private FacilityInfoMapper facilityInfoMapper;
        @Autowired
        private LabelInfoMapper labelInfoMapper;
        @Autowired
        private FeeValueMapper feeValueMapper;
        @Autowired
        private GraphInfoMapper graphInfoMapper;

    @Override
    public void saveOrUpdateInfo(ApartmentSubmitVo apartmentSubmitVo) {
        //获取公寓id
        Long id = apartmentSubmitVo.getId();

        //地址
        //查询公寓所属的省的信息(名字)
        //根据省份id查询
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        //查询公寓所属的城市名字
        CityInfo cityInfo = cityInfoMapper.selectById(apartmentSubmitVo.getCityId());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        //查询 公寓所属的区县名字
        DistrictInfo districtInfo = districtInfoMapper.selectById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());


        //添加或者修改基本信息:多着的内容它不会改
        this.saveOrUpdate(apartmentSubmitVo);

        if (id != null){
            //是修改
            //先全部删除
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType, ItemType.APARTMENT));
        }

        //添加公寓配套关系表
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            List<ApartmentFacility> apartmentFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(facilityInfoId).build();
                apartmentFacilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }

        //添加公寓标签关系
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(labelId).build();
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }

        //添加公寓杂费关系
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (feeValueIds != null){
            List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feeValueId).build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }

        //添加公寓的图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                String url = graphVo.getUrl();

                String fileName = url.substring(url.lastIndexOf("/") + 1);
                graphInfo.setName(fileName);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }


    }

    @Override
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(apartmentItemVoPage,queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //基本信息
        ApartmentInfo apartmentInfo = this.getById(id);

        //查询公寓的配套信息
        List<FacilityInfo> facilityInfoList=  facilityInfoMapper.selectListByApartmentId(id);
        //查询公寓的标签
        List<LabelInfo> labelInfoList=   labelInfoMapper.selectListByApartmentId(id);
        //查询公寓的杂费信息
        List<FeeValueVo> feeValueVoList=feeValueMapper.selectListByApartmentId(id);
        //查询公寓的图片:图片是一个单独表，里面已经有外键，但是要传入类型
        List<GraphVo> graphVoList=   graphInfoMapper.selectListByItemTypeAndItemId(id,ItemType.APARTMENT);

        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setGraphVoList(graphVoList);

        //基本信息对拷
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        return apartmentDetailVo;
    }

    @Override
    public void removeByIdInfo(Long id) {
        //查询当前被删除的公寓是否有房间 如果有房间提示不能删除
        Long count = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        if(count>0){
            //有房间,提示不能删除 抛自定义异常
            // throw  new LeaseException(ResultCodeEnum.DELETE_ERROR);
            throw  new LeaseException(208,"该公寓中有房间不能删除!!");
        }

        //删除公寓信息
        this.removeById(id);
        //删除关联的表
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType, ItemType.APARTMENT));

    }
}




